package com.xr.system.teacher.service.impl;

import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.xr.common.axios.AxiosEnum;
import com.xr.common.axios.AxiosResult;
import com.xr.common.exception.MyCustomException;
import com.xr.common.utils.ParallelStreamUtil;
import com.xr.system.teacher.mapper.TeacherMapper;
import com.xr.system.model.teacher.pojo.Teacher;
import com.xr.system.model.teacher.dto.TeacherDto;
import com.xr.system.teacher.service.ITeacherService;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * 教师信息表
 *
 * @author fzh
 * @email e_341877120_mail@163.com
 * @date 2022-09-30 10:21:20
 */
@Service
@Transactional
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements ITeacherService {

    /**
     * 根据条件查询列表
     *
     * @param teacherDto
     * @return
     */
    @Override
    public AxiosResult selectList(TeacherDto teacherDto) {
        LambdaQueryWrapper<Teacher> lqw;
        if (ObjectUtils.isEmpty(teacherDto)) {
            lqw = null;
        } else {
            lqw = new LambdaQueryWrapper<>();
        }
        List<Teacher> teacherList = baseMapper.selectList(lqw);
        boolean flag = ObjectUtils.isNotEmpty(teacherList);
        return flag ? AxiosResult.selectSuccess(teacherList) : AxiosResult.selectError();
    }

    /**
     * 根据条件分页查询
     *
     * @param currentPage
     * @param pageSize
     * @param teacherDto
     * @return
     */
    @Override
    public AxiosResult selectPage(int currentPage, int pageSize, TeacherDto teacherDto) {
        IPage<Teacher> iPage = new Page(currentPage, pageSize);
        LambdaQueryWrapper<Teacher> lqw;
        if (ObjectUtils.isEmpty(teacherDto)) {
            lqw = null;
        } else {
            lqw = new LambdaQueryWrapper<>();
        }
        iPage = page(iPage, lqw);
        boolean flag = ObjectUtils.isNotEmpty(iPage);
        return flag ? AxiosResult.selectSuccess(iPage) : AxiosResult.selectError();
    }

    /**
     * 根据id查询单个
     *
     * @param id
     * @return
     */
    @Override
    public AxiosResult selectOneById(Long id) {
        if (ObjectUtils.isNotEmpty(id)) {
            Teacher teacher = getById(id);
            boolean flag = ObjectUtils.isNotEmpty(teacher);
            return flag ? AxiosResult.selectSuccess(teacher) : AxiosResult.selectError();
        }
        throw new MyCustomException(AxiosEnum.PARAMETER_ERROR);
    }

    /**
     * 新增
     *
     * @param teacherDto
     * @return
     */
    @Override
    public AxiosResult insert(TeacherDto teacherDto) {
        if (ObjectUtils.isNotEmpty(teacherDto)) {
            boolean flag = save(teacherDto);
            return flag ? AxiosResult.insertSuccess() : AxiosResult.insertError();
        }
        throw new MyCustomException(AxiosEnum.PARAMETER_ERROR);
    }

    /**
     * 更新
     *
     * @param teacherDto
     * @return
     */
    @Override
    public AxiosResult update(TeacherDto teacherDto) {
        if (ObjectUtils.isNotEmpty(teacherDto)) {
            boolean flag = updateById(teacherDto);
            return flag ? AxiosResult.updateSuccess() : AxiosResult.updateError();
        }
        throw new MyCustomException(AxiosEnum.PARAMETER_ERROR);
    }

    /**
     * 根据id删除单个
     *
     * @param id
     * @return
     */
    @Override
    public AxiosResult delete(Long id) {
        if (ObjectUtils.isNotEmpty(id)) {
            boolean flag = removeById(id);
            return flag ? AxiosResult.deleteSuccess() : AxiosResult.deleteError();
        }
        throw new MyCustomException(AxiosEnum.PARAMETER_ERROR);
    }

    /**
     * 根据id集合批量删除
     *
     * @param idList
     * @return
     */
    @Override
    public AxiosResult deleteBatch(List<Long> idList) {
        if (ObjectUtils.isEmpty(idList) || idList.size() == 0) {
            throw new MyCustomException(AxiosEnum.PARAMETER_ERROR);
        }
        boolean flag = removeByIds(idList);
        return flag ? AxiosResult.deleteSuccess() : AxiosResult.deleteError();
    }

    /**
     * 导入文件
     *
     * @param file
     * @return
     */
    @Override
    public AxiosResult importFile(MultipartFile file) {
        boolean flag = false;
        try {
            InputStream inputStream = file.getInputStream();
            ExcelReader er = ExcelUtil.getReader(inputStream);
            List<Teacher> teacherList = er.readAll(Teacher.class);
            flag = ParallelStreamUtil.insertBatchOfLarge(teacherList, baseMapper::insertBatch);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag ? AxiosResult.success(AxiosEnum.IMPORT_SUCCESS) : AxiosResult.error(AxiosEnum.IMPORT_ERROR);
    }

    /**
     * 导出文件
     *
     * @param response
     */
    @Override
    public void exportFile(HttpServletResponse response) {
        try {
            List<Teacher> teacherList = list();
            ExcelWriter ew = ExcelUtil.getWriter(true);
            ew.setOnlyAlias(true);
            ew.write(teacherList);
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
            String fileName = URLEncoder.encode("Excel导出表", "UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
            ServletOutputStream sos = response.getOutputStream();
            ew.flush(sos, true);
            sos.close();
            IoUtil.close(sos);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
